<?php
/**
 *
 * Project:     Neuf TV MediaCenter - MP9CTL
 * File:        playlist.inc.php
 *
 * Sauvegarde des playlists
 *
 * @author Julien J. pour Neuf Cegetel
 *
 **/

class Track {
	private $album = FALSE;
	private $creator = FALSE;
	private $duration = FALSE;
	private $title = FALSE;
	private $location = FALSE;
	
	public function __construct() {
	
	}
	
	public function setAlbum($album) {
		$this->album = $album;
	}
	
	public function setCreator($creator) {
		$this->creator = $creator;
	}
	
	public function setDuration($duration) {
		$this->duration = $duration;
	}
	
	public function setTitle($title) {
		$this->title = $title;
	}
	
	public function setLocation($location) {
		$this->location = $location;
	}
	
	public function getPlaylistContent() {
		$return = "";
		if ($this->album) $return .= "\t\t\t<album>".$this->album."</album>";
		if ($this->creator) $return .= "\t\t\t<creator>".$this->creator."</creator>";
		if ($this->duration) $return .= "\t\t\t<duration>".$this->duration."</duration>";
		if ($this->title) $return .= "\t\t\t<title>".$this->title."</title>";
		if ($this->location) $return .= "\t\t\t<location>".$this->location."</location>";
		
		return $return;
	}
}

$trackList = array();

foreach ($_POST as $key => $value) {
	switch ($key) {
	case "playlist_name":
		$playlist_name = $_POST["playlist_name"];
		break;
	case "serviceId":
		$serviceId = $_POST["serviceId"];
		break;
	default:
		if (preg_match('/^(.*)([0-9]+)/', $key, $matches)) {
			switch ($matches[1]) {
			case "album":
				if (!isset($trackList[intval($matches[2])]) $trackList = new Track();
				$trackList[intval($matches[2])].setAlbum($value);
				break;
			case "creator":
				if (!isset($trackList[intval($matches[2])]) $trackList = new Track();
				$trackList[intval($matches[2])].setCreator($value);
				break;
			case "title":
				if (!isset($trackList[intval($matches[2])]) $trackList = new Track();
				$trackList[intval($matches[2])].setTitle($value);
				break;
			case "location":
				if (!isset($trackList[intval($matches[2])]) $trackList = new Track();
				$trackList[intval($matches[2])].setLocation($value);
				break;
			default:
				break;
			}
		}
		break;
	}
}

if (!isset($serviceId) || !isset($playlist_name) || !isset($_POST['location'])) $result = false;
else {
	$handler = fopen($_POST['location']."/".$playlist_name."xspf", "w");
	if (!$handler) $result = false;
	else {
		fwrite($handler, "<?xml version='1.0' encoding='iso-8859-1'?>\n");
		fwrite($handler, "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n");
		fwrite($handler, "\t<date>".date("dmY")."</date>\n");
		fwrite($handler, "\t<title>".$playlist_name."</title>\n");
		fwrite($handler, "\t<trackList>\n");
		foreach($trackList as $key => $value) {
			fwrite($handler, "\t\t<track>\n");
			fwrite($handler, $value->getPlaylistContent());
			fwrite($handler, "\t\t</track>\n");
		}
		fwrite($handler, "\t</trackList>\n");
		fwrite($handler, "</playlist>");
		
		$result = true;
	}
}

?>
<html>
	<head>
		<script language='Javascript' type='text/javascript'>
			function init() {
				if (typeof(opener.control.mediaCenter.servicesList["<?= $serviceId ?>"].playlistSaved == 'function')) 
					opener.control.mediaCenter.servicesList["<?= $serviceId ?>"].playlistSaved("<?= $result ?>"); 
			}
		</script>
	</head>
	<body onLoad='init()'>
	</body>
</html>
